package com.xuhuan.thread.study2;

/**
 * @author huan.xu
 * @date 2021/9/29 15:40
 * @description 线程优先级，线程是有cpu调度的，设置优先级并不能保证就按照优先级执行线程，而是提高概率
 */
public class ThreadPriorityDemo {
    public static void main(String[] args) {
        //主线程优先级
        System.out.println(Thread.currentThread().getName() + "优先级-->" + Thread.currentThread().getPriority());
        PriorityRunnable runnable = new PriorityRunnable();

        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        Thread t3 = new Thread(runnable);
        Thread t4 = new Thread(runnable);
        Thread t5 = new Thread(runnable);

        t1.start();

        t2.setPriority(1);
        t2.start();

        t3.setPriority(4);
        t3.start();

        t4.setPriority(7);
        t4.start();

        t5.setPriority(Thread.MAX_PRIORITY);
        t5.start();
    }
}

class PriorityRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "优先级-->" + Thread.currentThread().getPriority());
    }
}
